import json
# 这个东西在pycharm中找不到,需要在控制台中命令安装.pip3 install pygal_maps_world
from pygal_maps_world.i18n import COUNTRIES
from pygal_maps_world import maps
from pygal.style import RotateStyle,LightColorizedStyle


class WorldPopulation():

    def get_country_code(self, country_name):
        for code, name in COUNTRIES.items():
            if country_name == name:
                return code

        return None


world_pop = WorldPopulation()

filename = "population_data.json"
year_flag = "2010"

with open(filename) as file:
    json_datas = json.load(file)

country_pop = {}

for data in json_datas:
    if data["Year"] == year_flag:
        country_name = data["Country Name"]
        value = int(float(data["Value"]))
        code = world_pop.get_country_code(country_name)
        if code:
            country_pop[code] = value
        else:
            print("error " + country_name + " " + str(value))

# 根据人口数量分组
cc_pops_1 = {}
cc_pops_2 = {}
cc_pops_3 = {}

for code, value in country_pop.items():
    if value < 1000000:
        cc_pops_1[code] = value
    elif value < 1000000000:
        cc_pops_2[code] = value
    else:
        cc_pops_3[code] = value

# 看看每组分别包含多少个国家12
print(len(cc_pops_1), len(cc_pops_2), len(cc_pops_3))

wm_style=RotateStyle("#FF33CC",base_style=LightColorizedStyle)
# 直接使用明亮颜色
# wm_style=LightColorizedStyle
wm = maps.World(style=wm_style)
wm.title = 'World Population in 2010, by Country'
wm.add('1M', cc_pops_1)
wm.add("1M~1B", cc_pops_2)
wm.add("1B", cc_pops_3)

wm.render_to_file('world_population.svg')
